package com.baizhi.action;

import com.baizhi.entity.Book;
import com.baizhi.service.BookService;
import com.baizhi.service.impl.BookServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookAction {
    private Integer pageNum = 1;
    private Integer pageSize = 5;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void show() throws IOException {
        BookService bookService = new BookServiceImpl();
        List<Book> books = bookService.showBooks(pageNum, pageSize);
        PageInfo<Book> pageInfo = new PageInfo<>(books,10);
        int[] pages = pageInfo.getNavigatepageNums();

        //将books和pages响应回client
        ObjectMapper objectMapper = new ObjectMapper();
        //如果server端需要向client响应多部分数据,绝对不可以分别转换响应结果, 解决方案:将多部分数据封装到map中
        //[{...},{...},...]
        //objectMapper.writeValueAsString(books);
        //[1,2,3,...]
        //objectMapper.writeValueAsString(pages);
        Map<String, Object> map = new HashMap<>();
        map.put("books",books);
        map.put("pages",pages);
        String resultJson = objectMapper.writeValueAsString(map);

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        pw.print(resultJson);
        pw.flush();

    }

    private Integer bookId;

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public void remove() throws IOException {
        BookService bookService = new BookServiceImpl();

        Map<String, Object> map = new HashMap<>();
        try {
            bookService.removeBookById(bookId);
            map.put("status", "success");
        }catch(Exception e){
            e.printStackTrace();
            map.put("status", "failed");
        }

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        ObjectMapper objectMapper = new ObjectMapper();
        String resultJson = objectMapper.writeValueAsString(map);

        pw.print(resultJson);
        pw.flush();

    }

    private Book b;

    public Book getB() {
        return b;
    }

    public void setB(Book b) {
        this.b = b;
    }

    public void addBook() throws IOException {
        BookService bookService = new BookServiceImpl();
        Map<String, Object> map = new HashMap<>();
        try {
            bookService.addBook(b);
            map.put("status", "success");
        }catch(Exception e){
            e.printStackTrace();
            map.put("status", "failed");
        }

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        ObjectMapper objectMapper = new ObjectMapper();
        String resultJson = objectMapper.writeValueAsString(map);

        pw.print(resultJson);
        pw.flush();

    }

    public void showOneBook() throws IOException {
        BookService bookService = new BookServiceImpl();
        Book book = bookService.showBookById(bookId);

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        ObjectMapper objectMapper = new ObjectMapper();
        String bookJson = objectMapper.writeValueAsString(book);

        pw.print(bookJson);
        pw.flush();

    }

    public void updateBook() throws IOException {
        BookService bookService = new BookServiceImpl();
        Map<String, Object> map = new HashMap<>();
        try {
            bookService.updateBook(b);
            map.put("status", "success");
        }catch(Exception e){
            e.printStackTrace();
            map.put("status", "failed");
        }

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        ObjectMapper objectMapper = new ObjectMapper();
        String resultJson = objectMapper.writeValueAsString(map);

        pw.print(resultJson);
        pw.flush();

    }
}
